package com.wwwarehouse.usercenter.fragment.invite;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CardDeskTaskResponseBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.CodeEditText;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.response.AgreeAcceptInviteResponseBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * 同意邀请注册
 * Created by Jackie on 2017/8/9.
 */

@Route(path = UserCenterConstant.PATH_AGREE_2_ACCEPT_INVITE_FRAGMENT)
public class Agree2AcceptInviteFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;

    private View mRootLayout;
    private ImageView mHeadImg;
    private TextView mNameTxt;
    private CodeEditText mCodeEdt;
    private Button mCancelBtn;
    private Button mSureBtn;

    private CardDeskTaskResponseBean.TaskBean mTaskDetailsBean;

    private static final int REQUEST_GET_ACCEPT_INVITE = 0;
    private static final int REQUEST_REJECT_AGREE_INVITE = 1;
    private static final int REQUEST_ACCEPT_AGREE_INVITE = 2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_agree_2_accept_invite, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mRootLayout = findView(mRootView, R.id.rl_root);
        mHeadImg = findView(mRootView, R.id.iv_head);
        mNameTxt = findView(mRootView, R.id.tv_name);
        mCodeEdt = findView(mRootView, R.id.et_code);
        mCancelBtn = findView(mRootView, R.id.btn_cancel);
        mSureBtn = findView(mRootView, R.id.btn_sure);
    }

    private void initData() {
        Bundle bundle = getArguments();

        if (bundle != null) {
            mTaskDetailsBean = (CardDeskTaskResponseBean.TaskBean) bundle.getSerializable(Constant.KEY_CARD_DESK_TASK_DETAILS);

        }
    }

    private void initEvent() {
        mRootLayout.setOnClickListener(this);

        mCancelBtn.setOnClickListener(this);
        mSureBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.rl_root) {
            hideSoftKeyBoard(mRootLayout);
        } else if (id == R.id.btn_cancel) {
            rejectInvite();
        } else if (id == R.id.btn_sure) {
            acceptInvite();

        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof Agree2AcceptInviteFragment) {
            mActivity.setTitle(R.string.agree_invite);
        }
    }

    @Override
    public void requestDatas() {
        //查看同意接收邀请信息
        showProgressDialog(getString(R.string.waiting));

        Map map = new HashMap();
        map.put("cardUkid", mTaskDetailsBean.getCardUkid());

        NoHttpUtils.httpPost(UserCenterConstant.URL_GET_ACCEPT_INVITE, map, mOnResponseListener, REQUEST_GET_ACCEPT_INVITE);
    }

    private void rejectInvite() {
        showProgressDialog(getString(R.string.waiting));

        Map map = new HashMap();
        map.put("cardUkid", mTaskDetailsBean.getCardUkid());

        NoHttpUtils.httpPost(UserCenterConstant.URL_REJECT_AGREE_INVITE, map, mOnResponseListener, REQUEST_REJECT_AGREE_INVITE);
    }

    private void acceptInvite() {
        showProgressDialog(getString(R.string.waiting));

        Map map = new HashMap();
        map.put("codeType", "1");
        map.put("inviteCode", mCodeEdt.getText().toString().trim());

        NoHttpUtils.httpPost(UserCenterConstant.URL_CHECK_INVITE_CODE, map, mOnResponseListener, REQUEST_ACCEPT_AGREE_INVITE);
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_GET_ACCEPT_INVITE:
                    if ("0".equals(commonClass.getCode())) {
                        AgreeAcceptInviteResponseBean agreeAcceptInviteResponseBean = JSON.parseObject(commonClass.getData().toString(), AgreeAcceptInviteResponseBean.class);

                        BaseApplication.getApplicationInstance().displayRoundImgPixel(agreeAcceptInviteResponseBean.getFaceUrl(), mHeadImg, ConvertUtils.dip2px(mActivity, 80));
                        mNameTxt.setText(agreeAcceptInviteResponseBean.getInviteName());
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                case REQUEST_REJECT_AGREE_INVITE:
                    if ("0".equals(commonClass.getCode())) {
                        //返回桌面并刷新数据
                        EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                case REQUEST_ACCEPT_AGREE_INVITE:
                    if ("0".equals(commonClass.getCode())) {
                        EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
                    } else {
                        toast(commonClass.getMsg());
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            toast(result);
        }

        @Override
        public void onFinish(int what) {
            dismissProgressDialog();
        }
    };
}
